<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="layui-elem-field layui-field-title">
            <legend>添加院系</legend>
        </fieldset>
        <form class="layui-form" action="CollegeServlet" method="post"
        id="collegeAddForm" lay-filter="collegeAddFormFilter">
            <%--隐藏域，程序可读到--%>
            <input type="hidden" name="method" value="add">
            <div class="layui-form-item">
                <label class="layui-form-label">院系名称<span style="color: red">*</span></label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input"
                           id="name" name="name"
                           lay-verify="required|name" placeholder="请输入学院名或系名"
                           autocomplete="off" lay-reqtext="院系名称为必填项">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">院系编码<span style="color: red">*</span></label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input"
                           id="code" name="code"
                           lay-verify="required|code" placeholder="请输入学院编码"
                           autocomplete="off" lay-reqtext="院系编码为必填项">
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn " lay-submit="" lay-filter="collegeSaveBtn">添加院系</button>
                </div>
            </div>
        </form>

    </div>
</div>

<script>
    layui.use(['form','layer'], function () {

        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
        form.render();

        function ajaxVerify(verifyName,ajaxValue){
            let url = 'CollegeServlet';
            let params = {method:'verify',data:ajaxValue};
            let fn = function (res) {
                if (res == 'false') {
                    layer.msg(verifyName+"已经存在!");
                }
            }
            $.get(url, params, fn);
        }

        $("#name").blur(function () {
            var value = this.value;
            if (typeof value == "undefined" || value == null || value == "") {
                layer.msg("院系名称为必填项！", {icon: 5});
            } else if (/^[\u4e00-\u9fa5]{2,15}$/.test(value) == false) {
                layer.msg("学院名或系名为 2-15 个汉字!");
            }else{
                ajaxVerify("该院系名称", value);
            }
        })

        $("#code").blur(function () {
            var value = this.value;
            var reg = /^\d{1,11}$/;
            if (typeof value == "undefined" || value == null || value == "") {
                layer.msg("院系编号为必填项！", {icon: 5});
            } else if (reg.test(value) == false) {
                layer.msg("院系代号为1-11 位的数字!", {icon: 5});
            }
        })


        form.verify({
            name: function (value) {
                if (/^[\u4e00-\u9fa5]{2,15}$/.test(value) == false) {
                    return '学院名或系名为 2-15 个汉字'
                }

            },
            //不能有特殊字符和数字；可以输入英文，可以有空格，可以输入英文名字中的点，可以输入汉字，中文英文不能同时出现，长度在1-20
            code: function (value) {
                var reg = /^\d{1,11}$/;
                if (typeof value == "undefined" || value == null || value == "") {
                    return '院系编号为必填项！'
                } else if (reg.test(value) == false) {
                    return '院系代号为1-11 位的数字! '
                }
            },
        });

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;
        //监听提交
        form.on('submit(collegeSaveBtn)', function (data) {

            //通过AJAX提交数据到后台，根据后台的反馈做下一步的处理
            let url = "CollegeServlet";
            $.post(url,{method:data.field.method,name:data.field.name,code:data.field.code},function (res) {

                if(res=='true'){
                   //在查询院系界面，关闭添加院系界面
                    layer.close(parentIndex);
                    layer.msg("添加成功",{icon:1});
                    $('#collegeAddForm')[0].reset();
                    form.render();
                    if("undefined" != typeof parent){
                        parent.layui.table.reload('currentTableId');
                    }
                }
            })
            //return false;
        });

    })
</script>
